package com.second.app.thread.threadlocal;

import com.second.app.thread.threadlocal.tools.Tool;
import org.apache.commons.lang3.StringUtils;

/**
 * @Author GYZ
 * <pre>
 *
 * </pre>
 */
public class RunDemo {

    public static void main(String[] args) {
        new Thread(() -> {

            try {
                if (StringUtils.isEmpty(Tool.t1.get())) {
                    int value = (int) (Math.random() * 1000);
                    Tool.t1.set(value+"");
                    System.out.println(Thread.currentThread().getName() + "存入值：" + value);
                }
                Thread.sleep(1000);
                System.out.println(Thread.currentThread().getName() + "获取值：" + Tool.t1.get());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        },"线程A").start();

        new Thread(() -> {
            try {
                if (StringUtils.isEmpty(Tool.t1.get())) {
                    int value = (int) (Math.random() * 1000);
                    Tool.t1.set(value + "");
                    System.out.println(Thread.currentThread().getName() + "存入值：" + value);
                }
                Thread.sleep(1000);
                System.out.println(Thread.currentThread().getName() + "获取值：" + Tool.t1.get());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"线程B").start();
    }
}
